/*
This file uses  country_imp_aff2007_type.dta
	and the distance matrix to compute firm-by-country gravity measures

*/

 **Set directories
cd ""  /* PROJECT ROOT FOLDER */

/* DEFINE GLOBALS PATHS HERE */

global data "data"
global temp data/work
global junk data/junk
global distances "data/distances"
global input_country "input/country_data_input"
global input data

/* These are public data values from the CEPII gravity dataset  */
global gdpUSA 14477599703.04
global popUSA 301279.59
global wagehcadjUSA .4510375


** first - pull a dataset of wages
use $data/country_data_unique.dta, clear
gen avg_wage_usd_rel = avg_wage_usd / 3143  /*  3143 is from public data on Teresa Fort's website */
label var avg_wage_usd_rel "Average Nominal Monthly Wage in 2007 in 3143 USD (US wage)"
gen wage_hc_adj=avg_wage_usd_rel*exp(-.06*yr_sch)

* an adjustment for taiwan
* replacing it with Hong Kong wage
replace wage_hc_adj = 0.25414607 if iso3 == "TWN"  /*  Public data from CEPII gravity databse */

** adjusting wage to be gdppc if wage is missing
* normalizing such that it is relative to the US
* and if we put in the US gdp and population we get back the same HC adjusted wage
* need to adjust units, hence /1000
replace wage_hc_adj = gdp/pop / ( $gdpUSA / $popUSA) * $wagehcadjUSA / 1000 if wage_hc_adj == .

keep iso3 wage_hc_adj gdp



* dropping somalia
* we have its gdp here but not its gdp in the distance matrix
drop if iso3 == "SOM"

keep if wage_hc_adj != . & gdp != .
keep iso3 wage_hc_adj

save $junk/wage_hc_adj.dta, replace

** adjust the distance matrix
use $distances/distance_mat.dta, clear
ren iso3_o iso3
merge n:1 iso3 using $junk/wage_hc_adj.dta

drop if _merge ==1
drop _merge
ren wage_hc_adj wage_hc_adj_o


ren iso3 iso3_o

ren iso3_d iso3
merge n:1 iso3 using $junk/wage_hc_adj.dta
drop if _merge == 1
drop _merge

ren wage_hc_adj wage_hc_adj_d
ren iso3 iso3_d 

save $distances/distance_mat_abb.dta, replace

** adjust the US distance matrix
use $distances/distance_mat_USA.dta, clear
ren iso3_o iso3
merge n:1 iso3 using $junk/wage_hc_adj.dta

drop if _merge ==1
drop _merge
ren wage_hc_adj wage_hc_adj_o

ren iso3 iso3_o

ren iso3_d iso3
merge n:1 iso3 using $junk/wage_hc_adj.dta
drop if _merge == 2
drop _merge

ren iso3 iso3_d
ren wage_hc_adj wage_hc_adj_USA

save $distances/distance_mat_USA_abb.dta, replace



** make country list
use $distances/distance_mat_USA_abb.dta, clear
keep iso3_o 

ren iso3_o iso3

save $data/country_use_list.dta, replace


** first measure - simple measure for extensive margin, exporting
use firmid iso3 number_man_affiliates using $input/country_imp_aff2007.dta, clear

keep if number_man_aff >= 1 & number_man_aff != .
drop number

ren iso3 iso3_o
joinby iso3_o using $distances/distance_mat_abb.dta, unmatched(both)

* check: iso3_o if _merge == 2, iso3_o if _merge == 1
* former: countries in our dataset, our firms have no affils there
* latter: countries not in our dataset, firms have affils there

keep if _merge == 3
drop _merge


foreach x in harm cap simple arith {
	gen inv_dist_`x' = dist_`x'^(-1)
	* throwing away the iso3_o == iso3_d case
	replace inv_dist_`x' = 0 if iso3_o == iso3_d
		
}

* weighted measures
gen inv_dist_harm_wage = dist_harm^(-1) * wage_hc_adj_o^(-1)
replace inv_dist_harm_wage = 0 if iso3_o == iso3_d

gen inv_dist_harm_gdp = dist_harm^(-1) * gdp_o
replace inv_dist_harm_gdp = 0 if iso3_o == iso3_d



** now we have a dataset for each kprime \in K (iso3_o),
** the (inverse) distance to each i

** sum it all up for each destination
collapse(sum) inv_dist*, by(firmid iso3_d)

ren iso3_d iso3_o

* now merge in US info
merge n:1 iso3_o using $distances/distance_mat_USA_abb.dta
keep if _merge == 3
drop _merge


foreach x in harm cap simple arith {
	gen x_phi_i_`x' = inv_dist_`x' * dist_`x'_USA
	replace x_phi_i_`x' = log(1 + x_phi_i_`x')
	
}

gen x_phi_i_harm_wage = inv_dist_harm_wage * dist_harm_USA * wage_hc_adj_USA
replace x_phi_i_harm_wage = log(1+x_phi_i_harm_wage)

gen x_phi_i_harm_gdp = inv_dist_harm_gdp* dist_harm_USA / $gdpUSA
replace x_phi_i_harm_gdp = log(1+x_phi_i_harm_gdp)


keep firmid iso3_o x_phi_i*
ren iso3_o iso3

label var x_phi_i_simple "X_phi_i using simple distance"
label var x_phi_i_cap "X_phi_i using distance between capitals"
label var x_phi_i_harm "X_phi_i using harmonic distance"
label var x_phi_i_arith "X_phi_i using arithmetic distance"
label var x_phi_i_harm_gdp "X_phi_i using harmonic distance, with GDP weights"
label var x_phi_i_harm_wage "X_phi_i using harmonic distance, with wage weights"

save $junk/aff_weighted_inv_dist_simple.dta, replace





* let's do importers now
use firmid iso3 importer  using $input/country_imp_aff2007.dta, clear

* adding "USA" for all of them
preserve
duplicates drop firmid, force
keep firmid
gen iso3 = "USA"
gen importer = 1
gen exporter = 1
save $junk/us_firmids.dta, replace

restore


append using $junk/us_firmids

keep if importer == 1 
drop importer exporter




* merge on all possible j
ren iso3 iso3_o
joinby iso3_o using $distances/distance_mat_abb.dta, unmatched(both)

* again missing iso3 ATF

keep if _merge == 3
drop _merge

* making some things consistent
gen wage = wage_hc_adj_o^(-1)
gen unweight = 1
gen gdp = gdp_o

* also creating a version where we drop the USA



foreach x in unweight wage gdp {
	gen inv_dist_import_`x' = dist_harm^(-1) * `x'
	gen inv_dist_fta_import_`x' = fta * `x'
	gen inv_dist_lan_import_`x' = comlang_off * `x'
	gen inv_dist_leg_import_`x' = comleg_pretrans * `x'
	gen inv_dist_con_import_`x' = contig * `x'
	
	gen idist_imp_`x'_noUS = inv_dist_import_`x' if iso3_o != "USA"
	gen idist_fta_imp_`x'_noUS = inv_dist_fta_import_`x' if iso3_o != "USA"
	gen idist_lan_imp_`x'_noUS = inv_dist_lan_import_`x' if iso3_o != "USA"
	gen idist_leg_imp_`x'_noUS = inv_dist_leg_import_`x' if iso3_o != "USA"
	gen idist_con_imp_`x'_noUS = inv_dist_con_import_`x' if iso3_o != "USA"

}


* collapse over j for each possible kprime

collapse(sum) inv_dist* idist*, by(firmid iso3_d)

* renaming something to merge later
ren iso3_d iso3_o


save $junk/importer_inv_dist_sum.dta, replace

* also save a US only version
drop idist*
keep if iso3_o == "USA"

foreach x in unweight wage gdp {
	ren inv_dist_import_`x' inv_dist_import_`x'_USA
	ren inv_dist_fta_import_`x' inv_dist_fta_import_`x'_USA
	ren inv_dist_lan_import_`x' inv_dist_lan_import_`x'_USA
	ren inv_dist_con_import_`x' inv_dist_con_import_`x'_USA
	ren inv_dist_leg_import_`x' inv_dist_leg_import_`x'_USA

}

drop iso3_o 
save $junk/importer_inv_dist_sum_USA.dta, replace


* now we repeat the steps from above, but also merge in this importer inv distance dataset
use firmid iso3 number_man_affiliates using $input/country_imp_aff2007.dta, clear

keep if number_man_aff >= 1 & number_man_aff != .
drop number

ren iso3 iso3_o
joinby iso3_o using $distances/distance_mat_abb.dta

* now merge on kprime in iso3_o

merge n:1 firmid iso3_o using $junk/importer_inv_dist_sum.dta

* note that unmerged stuff from using is fine
* as we created the importer distance for every possible affiliate
drop if _merge == 2

* unmerged from master -> implies that a firm has affiliates, but does not import

* add in inv wage in the outside sum too!
gen wage = wage_hc_adj_o^(-1)
gen unweight = 1
gen gdp = gdp_o

foreach x in unweight wage gdp {
	replace inv_dist_import_`x' = 1 if inv_dist_import_`x' == .

	gen inv_dist_affil_`x' = dist_harm^(-1) * `x'

	replace inv_dist_affil_`x' = 0 if iso3_o == iso3_d

	gen inv_dist_sum_`x' = inv_dist_affil_`x' * inv_dist_import_`x'
}

collapse(sum) inv_dist_sum_*, by(firmid iso3_d)

keep firmid iso3_d inv_dist_*

label var iso3_d "destination location"
ren iso3_d iso3_o

* merge in US stuff
merge n:1 iso3_o using $distances/distance_mat_USA_abb.dta
keep if _merge == 3
drop _merge

gen unweight = 1
gen wage = wage_hc_adj_USA^(-1)
gen gdp = $gdpUSA

foreach x in unweight wage gdp {
	gen x_phi_k_i_`x' = inv_dist_sum_`x' * dist_harm_USA / `x'
}

* now the US import stuff
merge n:1 firmid using $junk/importer_inv_dist_sum_USA.dta

* again merge == 2 is fine
* these are firms that import that have no affiliates
drop if _merge == 2

* merge == 1 is firms that have affiliates but do not import
* for this we replace inv_dist_import_USA == 1
foreach x in unweight wage gdp {
	replace inv_dist_import_`x'_USA = 1 if _merge == 1
	replace x_phi_k_i_`x' = x_phi_k_i_`x' / inv_dist_import_`x'_USA
	replace x_phi_k_i_`x' = log(1 + x_phi_k_i_`x')
}

drop _merge

keep firmid iso3_o x_phi_k_i*

save $junk/aff_weighted_inv_dist_complex_import.dta, replace





* creating one final merged version
use $junk/aff_weighted_inv_dist_complex_import.dta, clear
ren iso3_o iso3

foreach x in unweight wage gdp {
	ren x_phi_k_i_`x' x_phi_i_`x'_withimport
}

label var x_phi_i_unweight_withimport "X_phi_i using harmonic distance, with importer weights"
label var x_phi_i_gdp_withimport "X_phi_i using harmonic distance, with importer weights and gdp"
label var x_phi_i_wage_withimport "X_phi_i using harmonic distance, with importer weights and wage"


merge 1:1 firmid iso3 using $junk/aff_weighted_inv_dist_simple.dta

drop _merge 


* drop the k subscript - it is just k = US
save $distances/aff_weighted_dist_measures_ext_export.dta, replace







** now extensive margin of importing 
* the simple measure is the same as the simple measure previously
* the complex measure is similar but also has a GDP weight on locations you export to
use firmid iso3 exporter number_man_aff using $input/country_imp_aff2007.dta, clear

append using $junk/us_firmids

keep if exporter == 1 | (number_man_aff > 0 & number_man_aff != .)
drop exporter number_man_aff importer

* merge on all possible j
ren iso3 iso3_o
joinby iso3_o using $distances/distance_mat_abb.dta, unmatched(both)

* again missing iso3 ATF
keep if _merge == 3
drop _merge

** this time need to add in GDP of the export location
gen inv_dist_export = dist_harm^(-1) * gdp_o
gen inv_dist_fta_export = fta * gdp_o
gen inv_dist_lan_export = comlang_off * gdp_o
gen inv_dist_con_export = contig * gdp_o
gen inv_dist_leg_export = comleg_pretrans * gdp_o

gen idist_exp_noUS = dist_harm^(-1) * gdp_o if iso3_o != "USA"
gen idist_fta_exp_noUS = fta * gdp_o  if iso3_o != "USA"
gen idist_lan_exp_noUS = comlang_off * gdp_o  if iso3_o != "USA"
gen idist_con_exp_noUS = contig * gdp_o  if iso3_o != "USA"
gen idist_leg_exp_noUS = comleg_pretrans * gdp_o  if iso3_o != "USA"


** need to make an extra thing for the exporter equivalent measure (no affiliates)
gen inv_dist_export_unweight = dist_harm^(-1) 
gen inv_dist_fta_export_unweight = fta 
gen inv_dist_lan_export_unweight = comlang_off 
gen inv_dist_con_export_unweight = contig 
gen inv_dist_leg_export_unweight = comleg_pretrans 

gen idist_exp_unweight_noUS = dist_harm^(-1)  if iso3_o != "USA"
gen idist_fta_exp_unweight_noUS = fta  if iso3_o != "USA"
gen idist_lan_exp_unweight_noUS = comlang_off  if iso3_o != "USA"
gen idist_con_exp_unweight_noUS = contig  if iso3_o != "USA"
gen idist_leg_exp_unweight_noUS = comleg_pretrans  if iso3_o != "USA"


* and also wage weighted
gen inv_dist_export_wage = dist_harm^(-1) *  wage_hc_adj_o^(-1)
gen inv_dist_fta_export_wage = fta *  wage_hc_adj_o^(-1)
gen inv_dist_lan_export_wage = comlang_off *  wage_hc_adj_o^(-1)
gen inv_dist_con_export_wage = contig *  wage_hc_adj_o^(-1)
gen inv_dist_leg_export_wage = comleg_pretrans *  wage_hc_adj_o^(-1)

gen idist_exp_wage_noUS = dist_harm^(-1) *  wage_hc_adj_o^(-1)  if iso3_o != "USA"
gen idist_fta_exp_wage_noUS = fta *  wage_hc_adj_o^(-1)  if iso3_o != "USA"
gen idist_lan_exp_wage_noUS = comlang_off *  wage_hc_adj_o^(-1)  if iso3_o != "USA" 
gen idist_con_exp_wage_noUS = contig *  wage_hc_adj_o^(-1) if iso3_o != "USA" 
gen idist_leg_exp_wage_noUS = comleg_pretrans *  wage_hc_adj_o^(-1)  if iso3_o != "USA"




* collapse over j for each possible kprime
collapse(sum) inv_dist* idist_*, by(firmid iso3_d)

* renaming something to merge later
ren iso3_d iso3_o

* it is annoying to re-create this, so let's just save it
save $junk/exporter_inv_dist_sum.dta, replace

* also save a US only version
keep if iso3_o == "USA"
drop idist_*

ren inv_dist_export inv_dist_export_USA
ren inv_dist_fta_export inv_dist_fta_export_USA
ren inv_dist_lan_export inv_dist_lan_export_USA
ren inv_dist_con_export inv_dist_con_export_USA
ren inv_dist_leg_export inv_dist_leg_export_USA

drop iso3_o
save $junk/exporter_inv_dist_sum_USA.dta, replace


* now we repeat the steps from above, but also merge in this exporter inv distance dataset
use firmid iso3 number_man_affiliates using $input/country_imp_aff2007.dta, clear

keep if number_man_aff >= 1 & number_man_aff != .
drop number

ren iso3 iso3_o
joinby iso3_o using $distances/distance_mat_abb.dta

* now merge on kprime in iso3_o

merge n:1 firmid iso3_o using $junk/exporter_inv_dist_sum.dta

* note that unmerged stuff from using is fine
* as we created the importer distance for every possible affiliate
drop if _merge == 2

* unmerged from master -> firms with affiliates but that do not import.  Replace any instances with 1
* need to also do this when merging in the USA distances

replace inv_dist_export = 1 if inv_dist_export == .

gen wage = wage_hc_adj_o^(-1)
gen unweight = 1
gen gdp = gdp_o

foreach x in unweight wage gdp {
	gen inv_dist_affil_`x' = dist_harm^(-1) * `x'
	replace inv_dist_affil_`x' = 0 if iso3_o == iso3_d
	
	gen inv_dist_sum_`x' = inv_dist_affil_`x' * inv_dist_export
}


collapse(sum) inv_dist_sum_*, by(firmid iso3_d)

keep firmid iso3_d inv_dist_*

label var iso3_d "destination location"
ren iso3_d iso3_o

* merge in US stuff
merge n:1 iso3_o using $distances/distance_mat_USA_abb.dta
keep if _merge == 3
drop _merge

gen wage = wage_hc_adj_USA^(-1)
gen unweight = 1
gen gdp = $gdpUSA

foreach x in unweight wage gdp {
	gen y_phi_k_i_`x' = inv_dist_sum_`x' * dist_harm_USA / `x'
}

* now the US import stuff
merge n:1 firmid using $junk/exporter_inv_dist_sum_USA.dta

* again merge == 2 is fine
* these are firms that import that have no affiliates
drop if _merge == 2

* merge == 1 is firms that have affiliates but do not import
* for this we replace inv_dist_import_USA == 1
foreach x in unweight wage gdp {
	replace inv_dist_export_USA = 1 if _merge == 1
	replace y_phi_k_i_`x' = y_phi_k_i_`x' / inv_dist_export_USA
	replace y_phi_k_i_`x' = log(1 + y_phi_k_i_`x')
}

drop _merge

keep firmid iso3_o y_phi_k_i*

save $junk/aff_weighted_inv_dist_complex_export.dta, replace




* creating one final merged version
use $junk/aff_weighted_inv_dist_complex_export.dta, clear
ren iso3_o iso3

foreach x in unweight wage gdp {
	ren y_phi_k_i_`x' y_phi_i_`x'_withexport
}

label var y_phi_i_unweight_withexport "Y_phi_i using harmonic distance, with exporter weights"
label var y_phi_i_wage_withexport "Y_phi_i using harmonic distance, with exporter weights and inverse wage"
label var y_phi_i_gdp_withexport "Y_phi_i using harmonic distance, with exporter weights and gdp"



merge 1:1 firmid iso3 using $junk/aff_weighted_inv_dist_simple.dta

label var x_phi_i_simple "Y_phi_i using simple distance"
label var x_phi_i_cap "Y_phi_i using distance between capitals"
label var x_phi_i_harm "Y_phi_i using harmonic distance"
label var x_phi_i_arith "Y_phi_i using arithmetic distance"
label var x_phi_i_harm_gdp "Y_phi_i using harmonic distance, with GDP weights"
label var x_phi_i_harm_wage "Y_phi_i using harmonic distance, with wage weights"


ren x_phi_i_simple y_phi_i_simple
ren x_phi_i_cap y_phi_i_cap
ren x_phi_i_harm y_phi_i_harm
ren x_phi_i_arith y_phi_i_arith 
ren x_phi_i_harm_gdp y_phi_i_harm_gdp
ren x_phi_i_harm_wage y_phi_i_harm_wage
drop _merge 


save $distances/aff_weighted_dist_measures_ext_import.dta, replace





* am going to do the FTA stuff separate, I think that is better
use firmid iso3 number_man_affiliates using $input/country_imp_aff2007.dta, clear

keep if number_man_aff >= 1 & number_man_aff != .
drop number

ren iso3 iso3_o
joinby iso3_o using $distances/distance_mat_abb.dta

* throw away the o = d case, not sure why some have FTA with themselves and others dont
replace fta = 0 if iso3_o == iso3_d
replace comleg_pretrans = 0 if iso3_o == iso3_d
replace comlang_off = 0 if iso3_o == iso3_d
replace contig = 0 if iso3_o == iso3_d

ren comleg_pretrans leg
ren comlang_off lan
ren contig con

* now merge on kprime in iso3_o
merge n:1 firmid iso3_o using $junk/importer_inv_dist_sum.dta
drop if _merge == 2
drop _merge

merge n:1 firmid iso3_o using $junk/exporter_inv_dist_sum.dta
drop if _merge == 2
drop _merge


foreach x in fta leg lan con{
	gen x_phi_i_`x' = `x' 
	gen x_phi_i_`x'_unweight_withimport = `x' * inv_dist_`x'_import_unweight
	gen x_phi_i_`x'_wage_withimport = `x' * wage_hc_adj_o^(-1) * inv_dist_`x'_import_wage
	gen x_phi_i_`x'_gdp_withimport = `x' * gdp_o * inv_dist_`x'_import_gdp

	gen y_phi_i_`x' = `x'
	gen y_phi_i_`x'_unweight_withexport = `x' * inv_dist_`x'_export
	gen y_phi_i_`x'_wage_withexport = `x' * wage_hc_adj_o^(-1) * inv_dist_`x'_export
	gen y_phi_i_`x'_gdp_withexport = `x' * gdp_o * inv_dist_`x'_export

}

collapse(sum) x* y*, by(firmid iso3_d)

foreach x in fta leg lan con{

	replace x_phi_i_`x' = log(1+x_phi_i_`x')
	replace y_phi_i_`x' = log(1+y_phi_i_`x')
}
label var x_phi_i_fta "x_phi_i using FTA"
label var y_phi_i_fta "y_phi_i using FTA"

label var x_phi_i_lan "x_phi_i using common language"
label var y_phi_i_lan "y_phi_i using common language"

label var x_phi_i_con "x_phi_i using contiguous"
label var y_phi_i_con "y_phi_i using contiguous"

label var x_phi_i_leg "x_phi_i using common legal system"
label var y_phi_i_leg "y_phi_i using common legal system"


merge n:1 firmid using $junk/importer_inv_dist_sum_USA.dta
drop if _merge == 2
drop _merge

* x_phi_i_fta is basically the number of affiliates you have with a FTA with i 
foreach x in fta leg lan con{
	replace x_phi_i_`x'_unweight_withimport = log(1 + x_phi_i_`x'_unweight_withimport / inv_dist_`x'_import_unweight_USA)
	replace x_phi_i_`x'_gdp_withimport = log(1 + x_phi_i_`x'_gdp_withimport /($gdpUSA *  inv_dist_`x'_import_gdp_USA))
	replace x_phi_i_`x'_wage_withimport = log(1 + x_phi_i_`x'_wage_withimport / (${wagehcadjUSA}^(-1)*inv_dist_`x'_import_wage_USA))
}

label var x_phi_i_fta_unweight_withimport "x_phi_i using FTA with import weights"
label var x_phi_i_fta_gdp_withimport "x_phi_i using FTA with import weights, gdp"
label var x_phi_i_fta_wage_withimport "x_phi_i using FTA with import weights, wage"

label var x_phi_i_lan_unweight_withimport "x_phi_i using common language with import weights"
label var x_phi_i_lan_gdp_withimport "x_phi_i using common language with import weights, gdp"
label var x_phi_i_lan_wage_withimport "x_phi_i using common language with import weights, wage"

label var x_phi_i_con_unweight_withimport "x_phi_i using contiguous with import weights"
label var x_phi_i_con_gdp_withimport "x_phi_i using contiguous with import weights, gdp"
label var x_phi_i_con_wage_withimport "x_phi_i using contiguous with import weights, wage"

label var x_phi_i_leg_unweight_withimport "x_phi_i using common legal system with import weights"
label var x_phi_i_leg_gdp_withimport "x_phi_i using common legal system with import weights, gdp"
label var x_phi_i_leg_wage_withimport "x_phi_i using common legal system with import weights, wage"

* x_phi_i_fta_withimport is the number of affiliaties you have with a FTA with i 
* where each affiliate is weighted by more based on the number of FTAs its host country has
* with each import source country

merge n:1 firmid using $junk/exporter_inv_dist_sum_USA.dta
drop if _merge == 2
drop _merge

foreach x in fta leg lan con{
	replace y_phi_i_`x'_unweight_withexport = log(1+ y_phi_i_`x'_unweight_withexport / inv_dist_`x'_export_USA)
	replace y_phi_i_`x'_gdp_withexport = log(1+ y_phi_i_`x'_gdp_withexport /($gdpUSA *  inv_dist_`x'_export_USA))
	replace y_phi_i_`x'_wage_withexport = log(1+ y_phi_i_`x'_wage_withexport /(${wagehcadjUSA}^(-1) *  inv_dist_`x'_export_USA))
}

label var y_phi_i_fta_unweight_withexport "y_phi_i using FTA with export weights"
label var y_phi_i_fta_wage_withexport "y_phi_i using FTA with export weights, wage"
label var y_phi_i_fta_gdp_withexport "y_phi_i using FTA with export weights, gdp"

label var y_phi_i_lan_unweight_withexport "y_phi_i using common language with export weights"
label var y_phi_i_lan_wage_withexport "y_phi_i using common language with export weights, wage"
label var y_phi_i_lan_gdp_withexport "y_phi_i using common language with export weights, gdp"

label var y_phi_i_con_unweight_withexport "y_phi_i using contiguous with export weights"
label var y_phi_i_con_wage_withexport "y_phi_i using contiguous with export weights, wage"
label var y_phi_i_con_gdp_withexport "y_phi_i using contiguous with export weights, gdp"

label var y_phi_i_leg_unweight_withexport "y_phi_i using common legal system with export weights"
label var y_phi_i_leg_wage_withexport "y_phi_i using common legal system with export weights, wage"
label var y_phi_i_leg_gdp_withexport "y_phi_i using common legal system with export weights, gdp"


ren iso3_d iso3

keep firmid iso3 x* y*
save $distances/aff_weighted_FTA_ext_import.dta, replace







* now measures for domestic firms
* this is just the inner sums divided by the US normalizations for wage, GDP
use $junk/exporter_inv_dist_sum.dta, clear
merge n:1 iso3_o using $distances/distance_mat_USA.dta
drop if _merge == 2
drop _merge

gen z_phi_i_export_gdp = log(1+idist_exp_noUS * dist_harm_USA / $gdpUSA )
gen z_phi_i_fta_export_gdp = log(1+idist_fta_exp_noUS / $gdpUSA )
gen z_phi_i_lan_export_gdp = log(1+idist_lan_exp_noUS / $gdpUSA )
gen z_phi_i_con_export_gdp = log(1+idist_con_exp_noUS / $gdpUSA )
gen z_phi_i_leg_export_gdp = log(1+idist_leg_exp_noUS / $gdpUSA )

gen unweight = 1
gen wage = 1/$wagehcadjUSA

foreach x in unweight wage {
	gen z_phi_i_export_`x' = log(1+idist_exp_`x'_noUS * dist_harm_USA / `x')
	gen z_phi_i_fta_export_`x' = log(1+idist_fta_exp_`x'_noUS / `x')
	gen z_phi_i_lan_export_`x' = log(1+idist_lan_exp_`x'_noUS / `x')
	gen z_phi_i_con_export_`x' = log(1+idist_con_exp_`x'_noUS / `x')
	gen z_phi_i_leg_export_`x' = log(1+idist_leg_exp_`x'_noUS / `x')
	
}

keep firmid iso3_o z*
ren iso3_o iso3

sort firmid iso3

save $distances/domestic_exporter_dist.dta, replace


use $junk/importer_inv_dist_sum.dta, clear
merge n:1 iso3_o using $distances/distance_mat_USA.dta
drop if _merge == 2
drop _merge

gen wage = 1/$wagehcadjUSA
gen unweight = 1
gen gdp = $gdpUSA

foreach x in unweight wage gdp {
	gen z_phi_i_import_`x' = log(1+idist_imp_`x' * dist_harm_USA / `x')
	gen z_phi_i_fta_import_`x' = log(1+idist_fta_imp_`x' / `x')
	gen z_phi_i_lan_import_`x' = log(1+idist_lan_imp_`x' / `x')
	gen z_phi_i_con_import_`x' = log(1+idist_con_imp_`x' / `x')
	gen z_phi_i_leg_import_`x' = log(1+idist_leg_imp_`x' / `x')
	
}

keep firmid iso3_o z*
ren iso3_o iso3

sort firmid iso3

save $distances/domestic_importer_dist.dta, replace



